package com.smarthospital.web.configuration;


import com.tencentyun.TLSSigAPIv2;
import com.wechat.pay.contrib.apache.httpclient.WechatPayHttpClientBuilder;
import com.wechat.pay.contrib.apache.httpclient.auth.AutoUpdateCertificatesVerifier;
import com.wechat.pay.contrib.apache.httpclient.auth.PrivateKeySigner;
import com.wechat.pay.contrib.apache.httpclient.auth.WechatPay2Credentials;
import com.wechat.pay.contrib.apache.httpclient.auth.WechatPay2Validator;
import com.wechat.pay.contrib.apache.httpclient.util.PemUtil;
import org.apache.http.impl.client.CloseableHttpClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.client.ClientConfiguration;
import org.springframework.data.elasticsearch.client.RestClients;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.security.PrivateKey;
import java.security.cert.X509Certificate;
import java.util.ArrayList;

/**
 * create by sym  2021/5/4 23:48.
 */
@Configuration
public class SHConfiguration {
    @Value("${wx.miniApp.appid}")
    private String appid;
    @Value("${wx.miniApp.mchid}")
    private String mchid;
    @Value("${wx.miniApp.merchantSerialNumber}")
    private String merchantSerialNumber;
    @Value("${wx.miniApp.apiV3Key}")
    private String apiV3Key;

    @Value("${tx.trtc.trtcSDKAppID:1400543485}")
    private long trtcSDKAppID;
    @Value("${tx.trtc.trtcSecreKey:738c02e2de58c7365e219eabadf584fa5b20df2a1703d33dc58e4c3ac6eb70c7}")
    private String trtcSecreKey;

    @Bean
    public ServerEndpointExporter serverEndpointExporter() {
        return new ServerEndpointExporter();
    }


    @Bean
    public RestHighLevelClient elasticsearchClient() {

        final ClientConfiguration clientConfiguration = ClientConfiguration.builder()
                .connectedTo("1.117.27.195:9200")
//                .withBasicAuth("","")
//                .connectedTo("127.0.0.1:9200")
                .build();

        return RestClients.create(clientConfiguration).rest();
    }

    @Bean
    public TLSSigAPIv2 getTLSSigAPIv2(){
        return new TLSSigAPIv2(trtcSDKAppID, trtcSecreKey);
    }


    @Bean
    public RestTemplate getRestTemplate(){
        RestTemplate restTemplate = new RestTemplate();
        restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
        return restTemplate;
    }

    /**
     * 微信商家apiV3证书 http客户端
     * @return
     * @throws FileNotFoundException
     * @throws UnsupportedEncodingException
     */
    @Bean("wxCertHttpClient")
    public CloseableHttpClient  wxCertHttpClient() throws IOException {
//        //加载本地密钥
        PrivateKey merchantPrivateKey = PemUtil.loadPrivateKey(SHConfiguration.class.getClassLoader().getResourceAsStream("apiclient_key.pem"));
        //自动更新签名
        AutoUpdateCertificatesVerifier verifier = new AutoUpdateCertificatesVerifier(
                new WechatPay2Credentials(mchid, new PrivateKeySigner(merchantSerialNumber, merchantPrivateKey)),
                apiV3Key.getBytes("utf-8"));

        X509Certificate wechatpayCertificate = PemUtil.loadCertificate(SHConfiguration.class.getClassLoader().getResourceAsStream("apiclient_cert.pem"));
        ArrayList<X509Certificate> listCertificates = new ArrayList<>();
        listCertificates.add(wechatpayCertificate);

        WechatPayHttpClientBuilder builder = WechatPayHttpClientBuilder.create()
                .withMerchant(mchid, merchantSerialNumber, merchantPrivateKey)
                .withWechatpay(listCertificates)
                .withValidator(new WechatPay2Validator(verifier));

        return builder.build();
    }





}
